package util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class DBUtil {
	private static String driver;
	private static String url;
	private static String user;
	private static String password;
	private static Connection conn;
	
	static{
			try {
				Properties properties=new Properties();
				InputStream in=Class.class.getResourceAsStream("/db.properties");
				properties.load(in);
				driver=properties.getProperty("driver");
				url=properties.getProperty("url");
				user=properties.getProperty("user");
				password=properties.getProperty("password");
				Class.forName(driver);
				conn=DriverManager.getConnection(url, user, password);
			} catch (ClassNotFoundException e) {
				
				e.printStackTrace();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
	}
	public static Connection getConnnection(){
		try {
			if(conn!=null&&conn.isClosed())
			{
				conn=DriverManager.getConnection(url, user, password);
			}
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
		
	}
	
}
